/**
 * C Implementation: set_missing
 *
 * Description: how to set a key as missing.
 * 如何将键设置为丢失。（?）
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "eccodes.h"

int sfx_set_missing()
{
    int err = 0, is_missing = 0;

    // 高斯网格？
    const char* infile  = "d:/sfxData/ECMWF/eccodes_test_data/reduced_gaussian_pressure_level.grib2";
    const char* outfile = "d:/temp/out_surface_level.grib2";

    FILE* in  = fopen(infile, "rb");
    FILE* out = fopen(outfile, "wb");

    // create a new handle from a message in a file
    codes_handle* h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);

    size_t str_len = 3;
    char str[]     = "sfc";
    CODES_CHECK(codes_set_string(h, "typeOfFirstFixedSurface", str, &str_len), 0);

    CODES_CHECK(codes_set_missing(h, "scaleFactorOfFirstFixedSurface"), 0);
    CODES_CHECK(codes_set_missing(h, "scaledValueOfFirstFixedSurface"), 0);

    // See GRIB-490
    long Ni = 0;
    CODES_CHECK(codes_get_long(h, "Ni", &Ni), 0);
    is_missing = codes_is_missing(h, "Ni", &err);
    CODES_CHECK(err, 0);
    if (is_missing != 1) assert(!"Ni should be missing");
    CODES_CHECK(codes_set_long(h, "Ni", Ni), 0);

    // get the coded message in a buffer
    const void* buffer = NULL;
    size_t size        = 0;
    CODES_CHECK(codes_get_message(h, &buffer, &size), 0);

    // write the buffer in a file
    if (fwrite(buffer, 1, size, out) != size) {
        perror(outfile);
    }


    codes_handle_delete(h);
    fclose(in);
    fclose(out);
    return 0;
}

/*
int main(int argc, char** argv)
{
    FILE* out           = NULL;
    codes_handle* h     = NULL;





    // delete handle 

    return 0;
}
*/
